package com.ohos.carbon.slice;

import com.ohos.carbon.LogUtils;
import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import com.ohos.carbon.provider.RecyclerAdapter;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

import static com.ohos.carbon.slice.MainAbilitySlice.preferences;


public class WhatsProvider extends RecyclerAdapter<WhatsProvider.ViewHolder> {

    private List<DataBean> mList;
    private Context context;


    public WhatsProvider(List<DataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;

    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_whats_list_provider, componentContainer, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ViewHolder viewHolder = holder;
        if (mList != null) {
            if (position == 0) {
                viewHolder.content.setMultipleLine(true);
                viewHolder.content.setText(mList.get(position).getText());
                viewHolder.content.setTextColor(Color.GRAY);
                viewHolder.content.setTextSize(38);
                viewHolder.icon.setVisibility(Component.HIDE);
                viewHolder.caveat.setVisibility(Component.HIDE);
                viewHolder.favorites.setVisibility(Component.HIDE);
                viewHolder.content.setPaddingLeft(130);
                viewHolder.content.setPaddingRight(110);
                viewHolder.content.setMarginTop(60);
            }
            Element icon = mList.get(position).getIcon();
            Element caveat = mList.get(position).getCaveat();
            Element favorites = mList.get(position).getFavorites();
            viewHolder.icon.setImageElement(icon);
            viewHolder.caveat.setImageElement(caveat);
            viewHolder.favorites.setImageElement(favorites);
            viewHolder.content.setText(mList.get(position).getText());
            viewHolder.content.setMultipleLine(true);
            viewHolder.content.setPaddingRight(110);

            viewHolder.favorites.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {

                    LogUtils.d("Darren", "favorites click");
                    LogUtils.d("Darren", "getText()" + mList.get(position).getText());
                    LogUtils.d("Darren", "preferences() before" + preferences.getBoolean(mList.get(position).getText(), false));
                    if (preferences.getBoolean(mList.get(position).getText(), false) == false) {
                        preferences.putBoolean(mList.get(position).getText(), true);
                        preferences.flushSync();
                        Element favour = ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_star_gray_24dp);
                        viewHolder.favorites.setImageElement(favour);
                    } else {
                        preferences.putBoolean(mList.get(position).getText(), false);
                        preferences.flushSync();
                        Element favour = ElementScatter.getInstance(context).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
                        viewHolder.favorites.setImageElement(favour);
                    }
                    LogUtils.d("Darren", "preferences() after" + preferences.getBoolean(mList.get(position).getText(), false));
                }
            });

        }

    }


    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int positon) {
        return positon;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {

        private Image icon, caveat, favorites;
        private Text content;

        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            icon = (Image) itemComponent.findComponentById(ResourceTable.Id_image);
            caveat = (Image) itemComponent.findComponentById(ResourceTable.Id_caveat);
            favorites = (Image) itemComponent.findComponentById(ResourceTable.Id_favorites);
            content = (Text) itemComponent.findComponentById(ResourceTable.Id_title_name);
        }
    }
}
